package rexsee.ebook;

import java.util.ArrayList;

import rexsee.core.style.StyleSheet;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Html;

public class Page {

	private final PageFactory mFactory;
	private final ArrayList<LineInterface> mLines;
	private final int mWidth;
	private final int mHeight;
	private final String mVerticalAlign;
	private final Rect mRect;
	private final Drawable mBackground;

	private final StyleSheet mHeaderStyle;
	private final String mHeaderText;
	private final Paint mHeaderPaint;
	private final boolean mHeaderLine;
	private final StyleSheet mFooterStyle;
	private final String mFooterText;
	private final Paint mFooterPaint;
	private final boolean mFooterLine;

	private int mContentHeight = 0;
	public ArrayList<String> indexTitles = new ArrayList<String>();
	public String inheriteTitle = null;
	public int index = 0;

	private String mText = "";

	public Page(PageFactory factory, PageBreakLine line) {
		this(factory, line.verticalAlign, line.rect, line.background, line.headerStyle, line.footerStyle);
	}
	public Page(PageFactory factory, String verticalAlign, Rect rect, Drawable bg, StyleSheet headerStyle, StyleSheet footerStyle) {

		mFactory = factory;
		mWidth = factory.mWidth;
		mHeight = factory.mHeight;
		mVerticalAlign = verticalAlign;
		mRect = rect;
		mBackground = bg;
		mLines = new ArrayList<LineInterface>();

		mHeaderStyle = headerStyle;
		if (headerStyle != null) {
			mHeaderText = headerStyle.label;
			mHeaderLine = headerStyle.horizontal_line.equalsIgnoreCase("true");
			mHeaderPaint = PageFactory.getPaint(headerStyle);
		} else {
			mHeaderText = null;
			mHeaderLine = false;
			mHeaderPaint = null;
		}
		mFooterStyle = footerStyle;
		if (footerStyle != null) {
			mFooterText = footerStyle.label;
			mFooterLine = footerStyle.horizontal_line.equalsIgnoreCase("true");
			mFooterPaint = PageFactory.getPaint(footerStyle);
		} else {
			mFooterText = null;
			mFooterLine = false;
			mFooterPaint = null;
		}
	}
	@Override
	public Page clone() {
		Page newPage = new Page(mFactory, mVerticalAlign, mRect, mBackground, mHeaderStyle, mFooterStyle);
		newPage.inheriteTitle = inheriteTitle;
		return newPage;
	}

	public boolean canAdd(LineInterface newLine) {
		return getContentHeight() + newLine.getHeight() < getRect().height();
	}
	public void add(LineInterface line) {
		mLines.add(line);
		mContentHeight += line.getHeight();
		if (line.getText() != null) mText += Html.fromHtml(line.getText()).toString();
	}
	public boolean isNull() {
		return mLines == null || mLines.size() == 0;
	}
	public Rect getRect() {
		return mRect;
	}
	public int getContentHeight() {
		return mContentHeight;
	}
	public Bitmap draw() {
		try {
			if (isNull()) return null;
			Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(bitmap);
			if (mBackground != null) mBackground.draw(canvas);
			int y;
			if (mVerticalAlign.equalsIgnoreCase("middle") || mVerticalAlign.equalsIgnoreCase("middle")) {
				y = mRect.top + Math.round((mRect.height() - mContentHeight) / 2);
			} else if (mVerticalAlign.equalsIgnoreCase("bottom")) {
				y = mRect.bottom - mContentHeight;
			} else {
				y = mRect.top;
			}
			for (int i = 0; i < mLines.size(); i++) {
				LineInterface line = mLines.get(i);
				Rect r = new Rect(mRect.left, y, mRect.right, y + line.getHeight());
				line.draw(canvas, r);
				y = r.bottom;
			}
			if (mHeaderPaint != null && mHeaderText != null && mHeaderText.length() > 0) {
				int hy;
				if (mHeaderLine) {
					canvas.drawLine(mRect.left, mRect.top - 12, mRect.right, mRect.top - 10, mHeaderPaint);
					hy = mRect.top - 22;
				} else {
					hy = mRect.top / 2 + (int) mHeaderPaint.getTextSize() / 2;
				}
				canvas.drawText(mHeaderText, TextLine.getOriginX(mRect, mHeaderPaint), hy, mHeaderPaint);
			}
			if (mFooterPaint != null) {
				String footer;
				if (mFooterText != null && mFooterText.length() > 0 && !mFooterText.equalsIgnoreCase("pagenumber")) {
					footer = mFooterText;
				} else {
					int total = mFactory.getTotal();
					String totalString = (total > 0) ? "/" + total : "";
					footer = (index + 1) + totalString;;
				}
				if (!footer.toLowerCase().equals("hidden")) {
					int fy;
					if (mFooterLine) {
						canvas.drawLine(mRect.left, mRect.bottom + 10, mRect.right, mRect.bottom + 12, mFooterPaint);
						fy = mRect.bottom + 16 + (int) mFooterPaint.getTextSize();
					} else {
						fy = mRect.bottom + (mHeight - mRect.bottom) / 2 + (int) mFooterPaint.getTextSize() / 2 - 2;
					}
					canvas.drawText(footer, TextLine.getOriginX(mRect, mFooterPaint), fy, mFooterPaint);
				}
			}
			return bitmap;
		} catch (Exception e) {
			return null;
		}
	}

	public boolean containsText(String text) {
		for (int i = 0; i < indexTitles.size(); i++) {
			if (indexTitles.get(i).contains(text)) return true;
		}
		return mText.contains(text);
	}
}
